<?php

namespace App\Http\Requests\PC\CustomerStore\Asset;

use App\Http\Requests\ApiRequest;
use App\Models\Customer\Asset;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property string code
 * @property string customer - route param customer_id
 * @property string asset - route param asset_id
 * @property UploadedFile model_xml
 * @property string bos_dir
 * @property string keep_name
 * @property string category_id
 * @property string|null version
 */
class AssetUpdateRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'code'        => [
                'required','string','min:1', 'max:'.self::MAX_STRING,
                Rule::unique((new Asset)->getTable(), 'code')
                    ->where($this->uniqueInCustomerAndCategory())
                    ->ignore($this->asset)
            ],
            'model_xml'   => 'required|file',
            'category_id' => 'required|int|min:0',
            'bos_dir'     => 'required|string|min:2',
            'keep_name'   => 'required|boolean',
            'version'     => 'nullable|int|min:0',
        ];
    }

    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'code.*'        => '素材的编号必须是字符串',
//            'model_xml.*'   => '素材文件必须是上传的xml文件',
//            'bos_dir.*'     => '文件名夹名称必须',
//            'keep_name.*'   => '必须提供是否保留原文件名参数',
//            'category_id.*' => '必须要提供分类ID',
//            'version.*'     => '版本号必须是大于0的整数',
//        ];
//    }

    /**
     * @return \Closure
     */
    private function uniqueInCustomerAndCategory(): \Closure
    {
        return function (Builder $query) {
            return $query
                ->where('customer_id', $this->customer)
                ->where('category_id', $this->category_id);
        };
    }
}
